

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.absa.inference.inference &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../../index.html">
          

          
            
            <img src="../../../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.absa.inference.inference</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.absa.inference.inference</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">PathLike</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>

<span class="kn">from</span> <span class="nn">nlp_architect.common.core_nlp_doc</span> <span class="kn">import</span> <span class="n">CoreNLPDoc</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.absa</span> <span class="kn">import</span> <span class="n">INFERENCE_OUT</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.absa.inference.data_types</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">Term</span><span class="p">,</span>
    <span class="n">TermType</span><span class="p">,</span>
    <span class="n">Polarity</span><span class="p">,</span>
    <span class="n">SentimentDoc</span><span class="p">,</span>
    <span class="n">SentimentSentence</span><span class="p">,</span>
    <span class="n">LexiconElement</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.absa.utils</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">_read_lexicon_from_csv</span><span class="p">,</span>
    <span class="n">load_opinion_lex</span><span class="p">,</span>
    <span class="n">_load_aspect_lexicon</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">INTENSIFIER_FACTOR</span> <span class="o">=</span> <span class="mf">0.3</span>
<span class="n">VERB_POS</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;VB&quot;</span><span class="p">,</span> <span class="s2">&quot;VBD&quot;</span><span class="p">,</span> <span class="s2">&quot;VBG&quot;</span><span class="p">,</span> <span class="s2">&quot;VBN&quot;</span><span class="p">,</span> <span class="s2">&quot;VBP&quot;</span><span class="p">,</span> <span class="s2">&quot;VBZ&quot;</span><span class="p">}</span>


<div class="viewcode-block" id="SentimentInference"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.inference.html#nlp_architect.models.absa.inference.inference.SentimentInference">[docs]</a><span class="k">class</span> <span class="nc">SentimentInference</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Main class for sentiment inference execution.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        opinion_lex: Opinion lexicon as outputted by TrainSentiment module.</span>
<span class="sd">        aspect_lex: Aspect lexicon as outputted by TrainSentiment module.</span>
<span class="sd">        intensifier_lex (dict): Pre-defined intensifier lexicon.</span>
<span class="sd">        negation_lex (dict): Pre-defined negation lexicon.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">aspect_lex</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">PathLike</span><span class="p">],</span>
        <span class="n">opinion_lex</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">PathLike</span><span class="p">,</span> <span class="nb">dict</span><span class="p">],</span>
        <span class="n">parse</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Inits SentimentInference with given aspect and opinion lexicons.&quot;&quot;&quot;</span>
        <span class="n">INFERENCE_OUT</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">opinion_lex</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">opinion_lex</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">dict</span> <span class="k">else</span> <span class="n">load_opinion_lex</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="n">opinion_lex</span><span class="p">))</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aspect_lex</span> <span class="o">=</span> <span class="n">_load_aspect_lexicon</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="n">aspect_lex</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">intensifier_lex</span> <span class="o">=</span> <span class="n">_read_lexicon_from_csv</span><span class="p">(</span><span class="s2">&quot;IntensifiersLex.csv&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">negation_lex</span> <span class="o">=</span> <span class="n">_read_lexicon_from_csv</span><span class="p">(</span><span class="s2">&quot;NegationSentLex.csv&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">parse</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">nlp_architect.pipelines.spacy_bist</span> <span class="kn">import</span> <span class="n">SpacyBISTParser</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">parser</span> <span class="o">=</span> <span class="n">SpacyBISTParser</span><span class="p">(</span><span class="n">spacy_model</span><span class="o">=</span><span class="s2">&quot;en&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">parser</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="SentimentInference.run"><a class="viewcode-back" href="../../../../../generated_api/nlp_architect.models.absa.inference.html#nlp_architect.models.absa.inference.inference.SentimentInference.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">doc</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">parsed_doc</span><span class="p">:</span> <span class="n">CoreNLPDoc</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">SentimentDoc</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Run SentimentInference on a single document.</span>

<span class="sd">        Returns:</span>
<span class="sd">            The sentiment annotated document, which contains the detected events per sentence.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">parsed_doc</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">parser</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Parser not initialized (try parse=True at init )&quot;</span><span class="p">)</span>
            <span class="n">parsed_doc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>

        <span class="n">sentiment_doc</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">sentence</span> <span class="ow">in</span> <span class="n">parsed_doc</span><span class="o">.</span><span class="n">sentences</span><span class="p">:</span>
            <span class="n">events</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">scores</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">aspect_row</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">aspect_lex</span><span class="p">:</span>
                <span class="n">_</span><span class="p">,</span> <span class="n">asp_events</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_event</span><span class="p">(</span><span class="n">aspect_row</span><span class="p">,</span> <span class="n">sentence</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">asp_event</span> <span class="ow">in</span> <span class="n">asp_events</span><span class="p">:</span>
                    <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">asp_event</span><span class="p">)</span>
                    <span class="n">scores</span> <span class="o">+=</span> <span class="p">[</span><span class="n">term</span><span class="o">.</span><span class="n">score</span> <span class="k">for</span> <span class="n">term</span> <span class="ow">in</span> <span class="n">asp_event</span> <span class="k">if</span> <span class="n">term</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">TermType</span><span class="o">.</span><span class="n">ASPECT</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">events</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">sentiment_doc</span><span class="p">:</span>
                    <span class="n">sentiment_doc</span> <span class="o">=</span> <span class="n">SentimentDoc</span><span class="p">(</span><span class="n">parsed_doc</span><span class="o">.</span><span class="n">doc_text</span><span class="p">)</span>
                <span class="n">sentiment_doc</span><span class="o">.</span><span class="n">sentences</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">SentimentSentence</span><span class="p">(</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;start&quot;</span><span class="p">],</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="s2">&quot;start&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">sentence</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="s2">&quot;len&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span>
                        <span class="n">events</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">sentiment_doc</span></div>

    <span class="k">def</span> <span class="nf">_extract_intensifier_terms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">toks</span><span class="p">,</span> <span class="n">sentiment_index</span><span class="p">,</span> <span class="n">polarity</span><span class="p">,</span> <span class="n">sentence</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Extract intensifier events from sentence.&quot;&quot;&quot;</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">terms</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">intens_i</span><span class="p">,</span> <span class="n">intens</span> <span class="ow">in</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">toks</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">intensifier_lex</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">math</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">sentiment_index</span> <span class="o">-</span> <span class="n">intens_i</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">intensifier_lex</span><span class="p">[</span><span class="n">intens</span><span class="p">]</span><span class="o">.</span><span class="n">score</span>
                <span class="n">terms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">Term</span><span class="p">(</span>
                        <span class="n">intens</span><span class="p">,</span>
                        <span class="n">TermType</span><span class="o">.</span><span class="n">INTENSIFIER</span><span class="p">,</span>
                        <span class="n">polarity</span><span class="p">,</span>
                        <span class="n">score</span><span class="p">,</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="n">intens_i</span><span class="p">][</span><span class="s2">&quot;start&quot;</span><span class="p">],</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="n">intens_i</span><span class="p">][</span><span class="s2">&quot;len&quot;</span><span class="p">],</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="n">count</span> <span class="o">+=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">score</span> <span class="o">+</span> <span class="nb">float</span><span class="p">(</span><span class="n">INTENSIFIER_FACTOR</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">count</span> <span class="k">if</span> <span class="n">count</span> <span class="o">!=</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">1</span><span class="p">,</span> <span class="n">terms</span>

    <span class="k">def</span> <span class="nf">_extract_neg_terms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">toks</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">op_i</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">sentence</span><span class="p">:</span> <span class="nb">list</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">tuple</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Extract negation terms from sentence.</span>

<span class="sd">        Args:</span>
<span class="sd">            toks: Sentence text broken down to tokens (words).</span>
<span class="sd">            op_i: Index of opinion term in sentence.</span>
<span class="sd">            sentence: parsed sentence</span>

<span class="sd">        Returns:</span>
<span class="sd">            List of negation terms and its aggregated sign (positive or negative).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sign</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">terms</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">gov_op_i</span> <span class="o">=</span> <span class="n">sentence</span><span class="p">[</span><span class="n">op_i</span><span class="p">][</span><span class="s2">&quot;gov&quot;</span><span class="p">]</span>
        <span class="n">dep_op_indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">sentence</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">sentence</span> <span class="k">if</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;gov&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">op_i</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">neg_i</span><span class="p">,</span> <span class="n">negation</span> <span class="ow">in</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">toks</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">negation_lex</span><span class="p">]:</span>
            <span class="n">position</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">negation_lex</span><span class="p">[</span><span class="n">negation</span><span class="p">]</span><span class="o">.</span><span class="n">position</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="n">op_i</span> <span class="o">-</span> <span class="n">neg_i</span>
            <span class="n">before</span> <span class="o">=</span> <span class="n">position</span> <span class="o">==</span> <span class="s2">&quot;before&quot;</span> <span class="ow">and</span> <span class="p">(</span><span class="n">dist</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">neg_i</span> <span class="ow">in</span> <span class="n">dep_op_indices</span><span class="p">)</span>
            <span class="n">after</span> <span class="o">=</span> <span class="n">position</span> <span class="o">==</span> <span class="s2">&quot;after&quot;</span> <span class="ow">and</span> <span class="p">(</span><span class="n">dist</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> <span class="n">neg_i</span> <span class="o">==</span> <span class="n">gov_op_i</span><span class="p">)</span>
            <span class="n">both</span> <span class="o">=</span> <span class="n">position</span> <span class="o">==</span> <span class="s2">&quot;both&quot;</span> <span class="ow">and</span> <span class="n">dist</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">before</span> <span class="ow">or</span> <span class="n">after</span> <span class="ow">or</span> <span class="n">both</span><span class="p">:</span>
                <span class="n">terms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">Term</span><span class="p">(</span>
                        <span class="n">negation</span><span class="p">,</span>
                        <span class="n">TermType</span><span class="o">.</span><span class="n">NEGATION</span><span class="p">,</span>
                        <span class="n">Polarity</span><span class="o">.</span><span class="n">NEG</span><span class="p">,</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">negation_lex</span><span class="p">[</span><span class="n">negation</span><span class="p">]</span><span class="o">.</span><span class="n">score</span><span class="p">,</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="n">toks</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">negation</span><span class="p">)][</span><span class="s2">&quot;start&quot;</span><span class="p">],</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="n">toks</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">negation</span><span class="p">)][</span><span class="s2">&quot;len&quot;</span><span class="p">],</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="n">sign</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">negation_lex</span><span class="p">[</span><span class="n">negation</span><span class="p">]</span><span class="o">.</span><span class="n">score</span>
        <span class="k">return</span> <span class="n">terms</span><span class="p">,</span> <span class="n">sign</span>

    <span class="k">def</span> <span class="nf">_extract_event</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">aspect_row</span><span class="p">:</span> <span class="n">LexiconElement</span><span class="p">,</span> <span class="n">parsed_sentence</span><span class="p">:</span> <span class="nb">list</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">tuple</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Extract opinion and aspect terms from sentence.&quot;&quot;&quot;</span>
        <span class="n">event</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sent_aspect_pair</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">real_aspect_indices</span> <span class="o">=</span> <span class="n">_consolidate_aspects</span><span class="p">(</span><span class="n">aspect_row</span><span class="o">.</span><span class="n">term</span><span class="p">,</span> <span class="n">parsed_sentence</span><span class="p">)</span>
        <span class="n">aspect_key</span> <span class="o">=</span> <span class="n">aspect_row</span><span class="o">.</span><span class="n">term</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">aspect_index_range</span> <span class="ow">in</span> <span class="n">real_aspect_indices</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">word_index</span> <span class="ow">in</span> <span class="n">aspect_index_range</span><span class="p">:</span>
                <span class="n">sent_aspect_pair</span><span class="p">,</span> <span class="n">event</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_detect_opinion_aspect_events</span><span class="p">(</span>
                    <span class="n">word_index</span><span class="p">,</span> <span class="n">parsed_sentence</span><span class="p">,</span> <span class="n">aspect_key</span><span class="p">,</span> <span class="n">aspect_index_range</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="n">sent_aspect_pair</span><span class="p">:</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="n">sent_aspect_pair</span><span class="p">,</span> <span class="n">event</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_modify_for_multiple_word</span><span class="p">(</span><span class="n">cur_tkn</span><span class="p">,</span> <span class="n">parsed_sentence</span><span class="p">,</span> <span class="n">index_range</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Modify multiple-word aspect tkn length and start index.</span>

<span class="sd">        Args:</span>
<span class="sd">            index_range: The index range of the multi-word aspect.</span>
<span class="sd">        Returns:</span>
<span class="sd">            The modified aspect token.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_range</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">cur_tkn</span><span class="p">[</span><span class="s2">&quot;start&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">parsed_sentence</span><span class="p">[</span><span class="n">index_range</span><span class="p">[</span><span class="mi">0</span><span class="p">]][</span><span class="s2">&quot;start&quot;</span><span class="p">]</span>
            <span class="n">cur_tkn</span><span class="p">[</span><span class="s2">&quot;len&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">parsed_sentence</span><span class="p">[</span><span class="n">index_range</span><span class="p">[</span><span class="mi">0</span><span class="p">]][</span><span class="s2">&quot;text&quot;</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">index_range</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
                <span class="n">cur_tkn</span><span class="p">[</span><span class="s2">&quot;len&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cur_tkn</span><span class="p">[</span><span class="s2">&quot;len&quot;</span><span class="p">])</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">parsed_sentence</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s2">&quot;text&quot;</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">cur_tkn</span>

    <span class="k">def</span> <span class="nf">_detect_opinion_aspect_events</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">aspect_index</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">,</span> <span class="n">aspect_key</span><span class="p">,</span> <span class="n">index_range</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Extract opinion-aspect events from sentence.</span>

<span class="sd">        Args:</span>
<span class="sd">            aspect_index: index of aspect in sentence.</span>
<span class="sd">            parsed_sent: current sentence parse tree.</span>
<span class="sd">            aspect_key: main aspect term serves as key in aspect dict.</span>
<span class="sd">            index_range: The index range of the multi word aspect.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List of aspect sentiment pair, and list of events extracted.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_pairs</span><span class="p">,</span> <span class="n">events</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="n">sentence_text_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">parsed_sent</span><span class="p">]</span>
        <span class="n">sentence_text</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sentence_text_list</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">tok_i</span><span class="p">,</span> <span class="n">tok</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">parsed_sent</span><span class="p">):</span>
            <span class="n">aspect_op_pair</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">terms</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">gov_i</span> <span class="o">=</span> <span class="n">tok</span><span class="p">[</span><span class="s2">&quot;gov&quot;</span><span class="p">]</span>
            <span class="n">gov</span> <span class="o">=</span> <span class="n">parsed_sent</span><span class="p">[</span><span class="n">gov_i</span><span class="p">]</span>
            <span class="n">gov_text</span> <span class="o">=</span> <span class="n">gov</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span>
            <span class="n">tok_text</span> <span class="o">=</span> <span class="n">tok</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span>

            <span class="c1"># 1st order rules</span>
            <span class="c1"># Is cur_tkn an aspect and gov an opinion?</span>
            <span class="k">if</span> <span class="n">tok_i</span> <span class="o">==</span> <span class="n">aspect_index</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">gov_text</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span><span class="p">:</span>
                    <span class="n">aspect_op_pair</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modify_for_multiple_word</span><span class="p">(</span><span class="n">tok</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">,</span> <span class="n">index_range</span><span class="p">),</span> <span class="n">gov</span><span class="p">)</span>
                    <span class="p">)</span>

            <span class="c1"># Is gov an aspect and cur_tkn an opinion?</span>
            <span class="k">if</span> <span class="n">gov_i</span> <span class="o">==</span> <span class="n">aspect_index</span> <span class="ow">and</span> <span class="n">tok_text</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span><span class="p">:</span>
                <span class="n">aspect_op_pair</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modify_for_multiple_word</span><span class="p">(</span><span class="n">gov</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">,</span> <span class="n">index_range</span><span class="p">),</span> <span class="n">tok</span><span class="p">)</span>
                <span class="p">)</span>

            <span class="c1"># If not found, try 2nd order rules</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">aspect_op_pair</span> <span class="ow">and</span> <span class="n">tok_i</span> <span class="o">==</span> <span class="n">aspect_index</span><span class="p">:</span>
                <span class="c1"># 2nd order rule #1</span>
                <span class="k">for</span> <span class="n">op_t</span> <span class="ow">in</span> <span class="n">parsed_sent</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">op_t</span><span class="p">[</span><span class="s2">&quot;gov&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">gov_i</span> <span class="ow">and</span> <span class="n">op_t</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span><span class="p">:</span>
                        <span class="n">aspect_op_pair</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modify_for_multiple_word</span><span class="p">(</span><span class="n">tok</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">,</span> <span class="n">index_range</span><span class="p">),</span> <span class="n">op_t</span><span class="p">)</span>
                        <span class="p">)</span>

                <span class="c1"># 2nd order rule #2</span>
                <span class="n">gov_gov</span> <span class="o">=</span> <span class="n">parsed_sent</span><span class="p">[</span><span class="n">parsed_sent</span><span class="p">[</span><span class="n">gov_i</span><span class="p">][</span><span class="s2">&quot;gov&quot;</span><span class="p">]]</span>
                <span class="k">if</span> <span class="n">gov_gov</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span><span class="p">:</span>
                    <span class="n">aspect_op_pair</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modify_for_multiple_word</span><span class="p">(</span><span class="n">tok</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">,</span> <span class="n">index_range</span><span class="p">),</span> <span class="n">gov_gov</span><span class="p">)</span>
                    <span class="p">)</span>

            <span class="c1"># if aspect_tok found</span>
            <span class="k">for</span> <span class="n">aspect</span><span class="p">,</span> <span class="n">opinion</span> <span class="ow">in</span> <span class="n">aspect_op_pair</span><span class="p">:</span>
                <span class="n">op_tok_i</span> <span class="o">=</span> <span class="n">parsed_sent</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">opinion</span><span class="p">)</span>
                <span class="n">score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opinion_lex</span><span class="p">[</span><span class="n">opinion</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span><span class="o">.</span><span class="n">score</span>
                <span class="n">neg_terms</span><span class="p">,</span> <span class="n">sign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_neg_terms</span><span class="p">(</span><span class="n">sentence_text_list</span><span class="p">,</span> <span class="n">op_tok_i</span><span class="p">,</span> <span class="n">parsed_sent</span><span class="p">)</span>
                <span class="n">polarity</span> <span class="o">=</span> <span class="n">Polarity</span><span class="o">.</span><span class="n">POS</span> <span class="k">if</span> <span class="n">score</span> <span class="o">*</span> <span class="n">sign</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="n">Polarity</span><span class="o">.</span><span class="n">NEG</span>
                <span class="n">intensifier_score</span><span class="p">,</span> <span class="n">intensifier_terms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_intensifier_terms</span><span class="p">(</span>
                    <span class="n">sentence_text_list</span><span class="p">,</span> <span class="n">op_tok_i</span><span class="p">,</span> <span class="n">polarity</span><span class="p">,</span> <span class="n">parsed_sent</span>
                <span class="p">)</span>
                <span class="n">over_all_score</span> <span class="o">=</span> <span class="n">score</span> <span class="o">*</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">intensifier_score</span>
                <span class="n">terms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">Term</span><span class="p">(</span>
                        <span class="n">aspect_key</span><span class="p">,</span>
                        <span class="n">TermType</span><span class="o">.</span><span class="n">ASPECT</span><span class="p">,</span>
                        <span class="n">polarity</span><span class="p">,</span>
                        <span class="n">over_all_score</span><span class="p">,</span>
                        <span class="n">aspect</span><span class="p">[</span><span class="s2">&quot;start&quot;</span><span class="p">],</span>
                        <span class="n">aspect</span><span class="p">[</span><span class="s2">&quot;len&quot;</span><span class="p">],</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="n">terms</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">Term</span><span class="p">(</span>
                        <span class="n">opinion</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">],</span>
                        <span class="n">TermType</span><span class="o">.</span><span class="n">OPINION</span><span class="p">,</span>
                        <span class="n">polarity</span><span class="p">,</span>
                        <span class="n">over_all_score</span><span class="p">,</span>
                        <span class="n">opinion</span><span class="p">[</span><span class="s2">&quot;start&quot;</span><span class="p">],</span>
                        <span class="n">opinion</span><span class="p">[</span><span class="s2">&quot;len&quot;</span><span class="p">],</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">neg_terms</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">terms</span> <span class="o">=</span> <span class="n">terms</span> <span class="o">+</span> <span class="n">neg_terms</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">intensifier_terms</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">terms</span> <span class="o">=</span> <span class="n">terms</span> <span class="o">+</span> <span class="n">intensifier_terms</span>
                <span class="n">all_pairs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="p">[</span><span class="n">aspect_key</span><span class="p">,</span> <span class="n">opinion</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">],</span> <span class="n">over_all_score</span><span class="p">,</span> <span class="n">polarity</span><span class="p">,</span> <span class="n">sentence_text</span><span class="p">]</span>
                <span class="p">)</span>
                <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">terms</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">all_pairs</span><span class="p">,</span> <span class="n">events</span></div>


<span class="k">def</span> <span class="nf">_sentence_contains_after</span><span class="p">(</span><span class="n">sentence</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">phrase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns sentence contains phrase after given index.&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">phrase</span><span class="p">)):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sentence</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">index</span> <span class="o">+</span> <span class="n">i</span> <span class="ow">or</span> <span class="n">phrase</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">{</span>
            <span class="n">sentence</span><span class="p">[</span><span class="n">index</span> <span class="o">+</span> <span class="n">i</span><span class="p">][</span><span class="n">field</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">,</span> <span class="s2">&quot;lemma&quot;</span><span class="p">)</span>
        <span class="p">}:</span>
            <span class="k">return</span> <span class="kc">False</span>
    <span class="k">return</span> <span class="kc">True</span>


<span class="k">def</span> <span class="nf">_consolidate_aspects</span><span class="p">(</span><span class="n">aspect_row</span><span class="p">,</span> <span class="n">sentence</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns consolidated indices of aspect terms in sentence.</span>

<span class="sd">    Args:</span>
<span class="sd">        aspect_row: List of aspect terms which belong to the same aspect-group.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">indices</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">aspect_phrases</span><span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span>
        <span class="p">[</span><span class="n">phrase</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">phrase</span> <span class="ow">in</span> <span class="n">aspect_row</span><span class="p">],</span> <span class="n">key</span><span class="o">=</span><span class="nb">len</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span>
    <span class="p">)</span>
    <span class="n">appeared</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">tok_i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sentence</span><span class="p">)):</span>
        <span class="k">for</span> <span class="n">aspect_phrase</span> <span class="ow">in</span> <span class="n">aspect_phrases</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">_sentence_contains_after</span><span class="p">(</span><span class="n">sentence</span><span class="p">,</span> <span class="n">tok_i</span><span class="p">,</span> <span class="n">aspect_phrase</span><span class="p">):</span>
                <span class="n">span</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">tok_i</span><span class="p">,</span> <span class="n">tok_i</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">aspect_phrase</span><span class="p">))</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">appeared</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="n">span</span><span class="p">):</span>
                    <span class="n">appeared</span> <span class="o">|=</span> <span class="nb">set</span><span class="p">(</span><span class="n">span</span><span class="p">)</span>
                    <span class="n">indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">span</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">indices</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>